package editAir.buildEdit
{
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.analysis.AnalysisServer;
	import _Pan3D.load.LoadInfo;
	import _Pan3D.load.LoadManager;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.Program3D;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	import newPan3d.content.build.NewBuildDisplay3DSprite;

	public class BuildEditDisplay3DSprite extends NewBuildDisplay3DSprite
	{
		private var _isChoose:Boolean=false;
		private var _isCanMove:Boolean=true;
		private var _chooseProgram:Program3D;
		public var name:String;

		public function BuildEditDisplay3DSprite(context:Context3D)
		{
			super(context);
			initChooseProgram();
		}
		override public function update() : void {
			if (!this._visible) {
				return;
			}
			if (_objData && _objData.texture) {
				
				if(_isChoose){
					_context.setProgram(_chooseProgram);
					setChooseVc();
					setChooseVa();
					resetChooseVa();
				}else{
					_context.setProgram(this.program);
					setVc();
					setVa();
					resetVa();
				}
				
			}
		}
		 protected function setChooseVc() : void {
			this.updateMatrix();
			//var angle:Vector3D = new Vector3D(0,1,-1.732,0);
			var angle:Vector3D = new Vector3D(Scene_data.sun3D.x,Scene_data.sun3D.y,Scene_data.sun3D.z,0);
			angle.normalize();
			
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 12, lightMatrix3D, true);
			
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,5,Vector.<Number>( [ 1,1,1,0.5 ])); //专门用来存树的通道的
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,Vector.<Number>([angle.x,angle.y,angle.z,angle.w]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,1,Vector.<Number>( [0.01,0,0,0.6 ]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,2, Vector.<Number>([0, 0, 0.5, 1]));
			
		}
		 protected function setChooseVa() : void {
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2,_objData.normalsBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setTextureAt(0,EditManage.buildEidtChooseText);
			_context.setTextureAt(1, _objData.texture);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);

			
		}
		 protected function resetChooseVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0,null);
			_context.setTextureAt(1,null);
		}
		
		private function initChooseProgram():void
		{
			_chooseProgram=Program3DManager.getInstance().getProgram(BuildEditDisplay3DSpriteShader.BUILD_EDIT_DISPLAY3D_SPRITE_SHADER);
		}

		public function get isChoose():Boolean
		{
			return _isChoose;
		}

		public function set isChoose(value:Boolean):void
		{
			_isChoose = value;
		}

		public function get isCanMove():Boolean
		{
			return _isCanMove;
		}

		public function set isCanMove(value:Boolean):void
		{
			_isCanMove = value;
		}
		
	
	}
}
